<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600753 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="1536"/>

<div>
<span><div><div>LBP 局部二值化</div><div><img src="LBP_files/Image.png" type="image/png" data-filename="Image.png"/></div><div>如果是一个眼睛图片局部二值化会用0和1画出一个眼睛</div><div>0/1的区别在于是否大于核心 </div><div>大于核心为1小于核心为0</div><div><a href="https://www.2cto.com/kf/201712/705037.html">https://www.2cto.com/kf/201712/705037.html</a><br/></div><div><br/></div><h1 style="letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">一、LBP特征的背景介绍</span></h1><div style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="border-width: 0px; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">LBP指局部二值模式，英文全称：Local Binary Pattern，是一种用来描述图像局部特征的算子，LBP特征具有灰度不变性和旋转不变性等显著优点。它是由T. Ojala, M.Pietik?inen, 和 D. Harwood [1][2]在1994年提出，由于LBP特征计算简单、效果较好，因此LBP特征在计算机视觉的许多领域都得到了广泛的应用，LBP特征比较出名的应用是用在人脸识别和目标检测中，在计算机视觉开源库Opencv中有使用LBP特征进行人脸识别的接口，也有用LBP特征训练目标检测分类器的方法，Opencv实现了LBP特征的计算，但没有提供一个单独的计算LBP特征的接口。</span></div><h1 style="letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">二、LBP特征的原理</span></h1><h3 style="border-width: 0px; padding: 0px; margin: 0px; list-style: none; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="border-width: 0px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">1、原始LBP特征描述及计算方法</span></h3><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"></p><div>原始的LBP算子定义在像素3*3的邻域内，以邻域中心像素为阈值，相邻的8个像素的灰度值与邻域中心的像素值进行比较，若周围像素大于中心像素值，则该像素点的位置被标记为1，否则为0。这样，3*3邻域内的8个点经过比较可产生8位二进制数，将这8位二进制数依次排列形成一个二进制数字，这个二进制数字就是中心像素的LBP值，LBP值共有28种可能，因此LBP值有256种。中心像素的LBP值反映了该像素周围区域的纹理信息。</div><div>备注：计算LBP特征的图像必须是灰度图，如果是彩色图，需要先转换成灰度图。</div><div>上述过程用图像表示为：</div><div><img src="LBP_files/Image.jpg" type="image/jpeg" data-filename="Image.jpg" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 630px; height: 167.514px;"/></div><div><img src="LBP_files/Image [1].png" type="image/png" data-filename="Image.png" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 506px; height: 143px;"/></div><div>将上述过程用公式表示为：</div><div><img src="LBP_files/Image [2].png" type="image/png" data-filename="Image.png" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 630px; height: 166.587px;"/></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">(xc,yc)为中心像素的坐标，p为邻域的第p个像素，ip为邻域像素的灰度值，ic为中心像素的灰度值，s(x)为符号函数</span></div><div style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="border-width: 0px; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">原始LBP特征计算代码(Opencv下)：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>//原始LBP特征计算</div><div>template &lt;typename _tp=&quot;&quot;&gt;</div><div>void getOriginLBPFeature(InputArray _src,OutputArray _dst)</div><div>{</div><div>    Mat src = _src.getMat();</div><div>    _dst.create(src.rows-2,src.cols-2,CV_8UC1);</div><div>    Mat dst = _dst.getMat();</div><div>    dst.setTo(0);</div><div>    for(int i=1;i&lt;src.rows-1;i++) _tp=&quot;&quot; center=&quot;src.at&lt;_tp&quot; int=&quot;&quot; j=&quot;1;j&lt;src.cols-1;j++)&quot;&gt;(i,j);</div><div>            unsigned char lbpCode = 0;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i-1,j-1) &gt; center) &lt;&lt; 7;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i-1,j  ) &gt; center) &lt;&lt; 6;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i-1,j+1) &gt; center) &lt;&lt; 5;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i  ,j+1) &gt; center) &lt;&lt; 4;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i+1,j+1) &gt; center) &lt;&lt; 3;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i+1,j  ) &gt; center) &lt;&lt; 2;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i+1,j-1) &gt; center) &lt;&lt; 1;</div><div>            lbpCode |= (src.at&lt;_tp&gt;(i  ,j-1) &gt; center) &lt;&lt; 0;</div><div>            dst.at&lt;uchar&gt;(i-1,j-1) = lbpCode;</div><div>        }</div><div>    }</div><div>}&lt;/uchar&gt;&lt;/src.rows-1;i++)&gt;&lt;/typename&gt;</div></div><div style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><br/></div><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"></p><div>测试结果：</div><div style="margin-top: 1em; margin-bottom: 1em;"><img src="LBP_files/Image [1].jpg" type="image/jpeg" data-filename="Image.jpg" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 537px; height: 296px;"/></div><h3 style="border-width: 0px; padding: 0px; margin: 0px; list-style: none; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="border-width: 0px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">2、LBP特征的改进版本</span></h3><div style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="border-width: 0px; text-indent: 2em; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">在原始的LBP特征提出以后，研究人员对LBP特征进行了很多的改进，因此产生了许多LBP的改进版本。</span></div><h4 style="font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"><span style="font-size: 14px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249); color: rgb(51, 51, 51); font-family: Simsun; font-variant-caps: normal; font-variant-ligatures: normal;">2.1 圆形LBP特征(Circular LBP or Extended LBP)</span></h4><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"></p><div>由于原始LBP特征使用的是固定邻域内的灰度值，因此当图像的尺度发生变化时，LBP特征的编码将会发生错误，LBP特征将不能正确的反映像素点周围的纹理信息，因此研究人员对其进行了改进[3]。基本的 LBP 算子的最大缺陷在于它只覆盖了一个固定半径范围内的小区域，这显然不能满足不同尺寸和频率纹理的需要。为了适应不同尺度的纹理特征，并达到灰度和旋转不变性的要求，Ojala 等对 LBP 算子进行了改进，将 3×3 邻域扩展到任意邻域，并用圆形邻域代替了正方形邻域，改进后的 LBP 算子允许在半径为 R 的圆形邻域内有任意多个像素点。从而得到了诸如半径为R的圆形区域内含有P个采样点的LBP算子：</div><div><img src="LBP_files/Image [2].jpg" type="image/jpeg" data-filename="Image.jpg" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 630px; height: 238.623px;"/></div><div>这种LBP特征叫做Extended LBP，也叫Circular LBP。使用可变半径的圆对近邻像素进行编码，可以得到如下的近邻：</div><div><img src="LBP_files/Image [3].png" type="image/png" data-filename="Image.png" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 393px; height: 105px;"/></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">对于给定中心点(xc,yc)，其邻域像素位置为(xp,yp)，p∈P，其采样点(xp,yp)用如下公式计算：</span></div><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(249, 249, 249);"></p><div><img src="LBP_files/Image [4].png" type="image/png" data-filename="Image.png" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 592px; height: 129px;"/></div><div>R是采样半径，p是第p个采样点，P是采样数目。由于计算的值可能不是整数，即计算出来的点不在图像上，我们使用计算出来的点的插值点。目的的插值方法有很多，Opencv使用的是双线性插值，双线性插值的公式如下：</div><div><img src="LBP_files/Image [5].png" type="image/png" data-filename="Image.png" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 462px; height: 69px;"/></div><div>通过LBP特征的定义可以看出，LBP特征对光照变化是鲁棒的，其效果如下图所示：</div><div style="margin-top: 1em; margin-bottom: 1em;"><img src="LBP_files/Image [3].jpg" type="image/jpeg" data-filename="Image.jpg" style="border-width: 0px; padding: 0px; margin: 0px auto; list-style: none; display: block; max-width: 630px; cursor: pointer; width: 630px; height: 358.75px;"/></div></div><div><br/></div></span>
</div></body></html> 